package com.fx.device.controller.device;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fx.device.common.util.ErrorEnum;
import com.fx.device.common.util.ReturnMessage;
import com.fx.device.common.util.ReturnMessageUtils;
import com.fx.device.request.device.*;
import com.fx.device.service.device.DeviceMaintenanceService;
import com.fx.device.vo.device.DeviceMaintenanceRecord;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;


@RestController
@RequestMapping(value = "/deviceMaintenanceRecord")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class DeviceMaintenanceController {
    final private DeviceMaintenanceService service;


    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ReturnMessage<Object> add(@RequestBody @Valid DeviceMaintenanceAddRequest request, BindingResult br) {
        log.info("Request:{}", request);
        if (request == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return service.add(request);
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ReturnMessage<Object> update(@RequestBody @Valid DeviceMaintenanceUpdateRequest request, BindingResult br) {
        log.info("Request:{}", request);
        if (request == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return service.update(request);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ReturnMessage<Object> update(@RequestBody @Valid DeviceMaintenanceDelRequest request, BindingResult br) {
        log.info("Request:{}", request);
        if (request == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return service.delete(request);
    }

    @RequestMapping(value = "/getById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ReturnMessage<DeviceMaintenanceRecord> getById(@RequestBody @Valid DeviceMaintenanceGetByIdRequest request, BindingResult br) {
        log.info("Request:{}", request);
        if (request == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return service.getById(request);
    }


    @RequestMapping(value = "/query", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ReturnMessage<IPage<DeviceMaintenanceRecord>> query(@RequestBody @Valid DeviceMaintenanceQueryRequest request, BindingResult br) {
        log.info("Request:{}", request);
        if (request == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return service.query(request);
    }














}
